<template>
  <el-container class="index-container">
    <!-- 头部 -->
    <el-header>
      <div class="left">
        <img :src="getImg('logo.png')" alt="" />
        <!-- <span>欢迎使用校园一卡通服务平台后台管理系统</span> -->
      </div>
      <el-button type="primary" @click="logout">退出登录</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse()">|||</div>
        <el-menu
          router
          unique-opened
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          :collapse="this.isCollapse"
          :collapse-transition="false"
          :default-active="this.activePath"
        >
          <el-submenu :index="item.id + ''" v-for="item in this.menuList" :key="item.id">
            <template slot="title">
              <i :class="item.icon"></i>
              <span> {{ item.authName }} </span>
            </template>
            <el-menu-item
              :index="subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState(subItem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span> {{ subItem.authName }} </span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { getImg } from '../../utils/getImg'
import staticServe from '../../api/services/static.serve'
export default {
  data() {
    return {
      // 左侧菜单数据
      menuList: [],
      isCollapse: false,
      activePath: ''
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    getImg,
    logout() {
      // 退出登录清除session
      window.sessionStorage.clear()
      this.$router.push('/')
    },
    // 获取左侧菜单数据
    getMenuList() {
      staticServe.getMenuList().then((res) => {
        console.log(res, 123)
        this.menuList = res
      })
    },
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.index-container {
  height: 100%;
  .el-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 70px !important;
    background-color: #373d41;
    .left {
      display: flex;
      align-items: center;
      span {
        padding-left: 20px;
        color: #fff;
        font-size: 24px;
      }
    }
  }
  .el-container {
    height: 100%;
    .el-aside {
      background-color: #333744;
      .toggle-button {
        background-color: #4a5064;
        color: #fff;
        font-size: 10px;
        font-weight: bold;
        line-height: 24px;
        text-align: center;
        letter-spacing: 0.2em;
        cursor: pointer;
      }
      .el-menu {
        border-right: none;
      }
    }
    .el-main {
      background-color: #eaedf1;
    }
  }
}
</style>
